//
//  YunDanCenterViewController.m
//  Rabbit
//
//  Created 阿帕 on 2019/12/16.
//  Copyright © 2019 GCY. All rights reserved.
//

#import "YunDanCenterViewController.h"

#import "YunDanCenterDetailViewController.h"

#import "ZhuangHuoViewController.h"

#import "PaiDanViewController.h"

#import "TouSuHuoZhuViewController.h"

#import "TouSuListViewController.h"

#import "TouSuDetailViewController.h"

#import "PingJiaViewController.h"

#import "PingJiaDetailViewController.h"

#import "YunDanCenterCell.h"

#import "DriverVehicleModel.h"

#import "HuoYuanDetailViewController.h"

#import "YnuDanBatchPaiDViewController.h"

#import "ZTXYiChangChuLiListController.h"



typedef void(^VehileCodeBlock)(NSString *vehicleCode);

@interface YunDanCenterViewController ()<UITableViewDelegate, UITableViewDataSource, YunDanListButtonDelegate, CancelAlertViewDeletage, CYNormalAlertViewEventDelegate>
{
    NSString *_statusStr;
    NSString *_orderCode;
}
@property (strong , nonatomic)UITableView *tableView;

@property (strong , nonatomic)NSMutableArray *dataArray;

@property (strong , nonatomic)NSMutableArray *menuArray;

@property (strong , nonatomic)UIImageView *foundImageView;

@property (nonatomic, assign) NSInteger pageNum;

@property (nonatomic, strong) NSMutableDictionary *notDic;

@property (nonatomic, strong) UIButton *allButton;




@end

static NSString *const YunDanCenterCellID = @"yunDanCell";

static const CGFloat cellHeight = 220;

@implementation YunDanCenterViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)dealloc {
    [KNotificationCenter removeObserver:self name:YUNDANCENTERLIST_REFRESH object:nil];
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.notDic = [NSMutableDictionary dictionary];
    [self.notDic setValue:@"" forKey:@"shipmentName"];
    [self.notDic setValue:@"" forKey:@"vehicleLicense"];
    [self.notDic setValue:@"" forKey:@"placeOfShipment"];
    [self.notDic setValue:@"" forKey:@"placeOfReceipt"];
    [self.notDic setValue:@"" forKey:@"lastLoadingTimeStart"];
    [self.notDic setValue:@"" forKey:@"lastLoadingTimeEnd"];
    [self.notDic setValue:@"" forKey:@"lastUnloadingTimeStart"];
    [self.notDic setValue:@"" forKey:@"lastUnloadingTimeEnd"];
    [KNotificationCenter addObserver:self selector:@selector(refreshListRequest:) name:YUNDANCENTERLIST_REFRESH object:nil];

    self.view.backgroundColor = BackgroundColor;

    _pageNum = 1;
    
    [self setupBaseView];
    
    [self yunDanListRequest:nil];
}

- (void)refreshListRequest:(NSNotification *)notif {
    [self.notDic setValue:notif.object[@"shipmentName"] forKey:@"shipmentName"];
    [self.notDic setValue:notif.object[@"vehicleLicense"] forKey:@"vehicleLicense"];
    [self.notDic setValue:notif.object[@"placeOfShipment"] forKey:@"placeOfShipment"];
    [self.notDic setValue:notif.object[@"placeOfReceipt"] forKey:@"placeOfReceipt"];
    [self.notDic setValue:notif.object[@"lastLoadingTimeStart"] forKey:@"lastLoadingTimeStart"];
    [self.notDic setValue:notif.object[@"lastLoadingTimeEnd"] forKey:@"lastLoadingTimeEnd"];
//    [self.notDic setValue:notif.object[@"lastLoadingTimeStart"] forKey:@"lastLoadingTimeStart"];
//    [self.notDic setValue:notif.object[@"lastLoadingTimeEnd"] forKey:@"lastLoadingTimeEnd"];
//    [self.notDic setValue:notif.object[@"lastUnloadingTimeStart"] forKey:@"lastUnloadingTimeStart"];
//    [self.notDic setValue:notif.object[@"lastUnloadingTimeEnd"] forKey:@"lastUnloadingTimeEnd"];
    self.pageNum = 1;
    
    [self yunDanListRequest:self.notDic];
//    [self.tableView.mj_header beginRefreshing];
}

- (void)yunDanListRequest:(NSMutableDictionary *)dic {
    WEAKSELF
    if (_pageNum == 1) {
        [self.dataArray removeAllObjects];
    }
    
    NSDictionary *params = [NSDictionary dictionary];
    
    if ([_orderStatus isEqual:@"6"]) {
        params = @{
            @"pageNum": [NSNumber numberWithInteger:_pageNum],
            @"pageSize": DEFULT_LOAD_NUM,
            @"deleted" : @"1",
            @"token":[UserDefaultUtil valueForKey:USER_TOKEN],
            @"shipmentName":self.notDic[@"shipmentName"],
            @"vehicleLicense":self.notDic[@"vehicleLicense"],
            @"placeOfShipment":self.notDic[@"placeOfShipment"],
            @"placeOfReceipt":self.notDic[@"placeOfReceipt"],
            @"gmtCreatedStart":self.notDic[@"lastLoadingTimeStart"],
            @"gmtCreatedEnd":self.notDic[@"lastLoadingTimeEnd"],
//            @"lastUnloadingTimeStart":self.notDic[@"lastUnloadingTimeStart"],
//            @"lastUnloadingTimeEnd":self.notDic[@"lastUnloadingTimeEnd"],
        };
        
    } else if ([_orderStatus isEqual:@"7"] || [_orderStatus isEqual:@"0"] || [_orderStatus isEqual:@"1"]) {
        //待派车、全部、待运输
        if ([_orderStatus isEqual:@"7"]) {
            //            _assignmentStatus = @"waitCar";//waitCar
            _orderStatus = @"1";//1
        }
        params = @{
            @"pageNum" : [NSNumber numberWithInteger:_pageNum],
            @"pageSize" : DEFULT_LOAD_NUM,
            @"status" : _orderStatus,//运单状态：1待运输 2运输中 3待结算 4待打款 5已完成
            @"assignmentStatus" : _assignmentStatus,
            @"token" : [UserDefaultUtil valueForKey:USER_TOKEN],
            @"shipmentName":self.notDic[@"shipmentName"],
            @"vehicleLicense":self.notDic[@"vehicleLicense"],
            @"placeOfShipment":self.notDic[@"placeOfShipment"],
            @"placeOfReceipt":self.notDic[@"placeOfReceipt"],
            @"gmtCreatedStart":self.notDic[@"lastLoadingTimeStart"],
            @"gmtCreatedEnd":self.notDic[@"lastLoadingTimeEnd"],
//            @"lastUnloadingTimeStart":self.notDic[@"lastUnloadingTimeStart"],
//            @"lastUnloadingTimeEnd":self.notDic[@"lastUnloadingTimeEnd"],
        };
        
    } else {
        if ([_orderStatus isEqual:@"8"])
        {
            _orderStatus = @"6";
        }
        params = @{
            @"pageNum": [NSNumber numberWithInteger:_pageNum],
            @"pageSize": DEFULT_LOAD_NUM,
            @"status" : _orderStatus,//运单状态：1待运输 2运输中 3待结算 4待打款 5已完成
            @"token":[UserDefaultUtil valueForKey:USER_TOKEN],
            @"shipmentName":self.notDic[@"shipmentName"],
            @"vehicleLicense":self.notDic[@"vehicleLicense"],
            @"placeOfShipment":self.notDic[@"placeOfShipment"],
            @"placeOfReceipt":self.notDic[@"placeOfReceipt"],
            @"gmtCreatedStart":self.notDic[@"lastLoadingTimeStart"],
            @"gmtCreatedEnd":self.notDic[@"lastLoadingTimeEnd"],
//            @"lastUnloadingTimeStart":self.notDic[@"lastUnloadingTimeStart"],
//            @"lastUnloadingTimeEnd":self.notDic[@"lastUnloadingTimeEnd"],
            
        };
        if ([_orderStatus isEqual:@"6"])
        {
            _orderStatus = @"8";
        }
    }
    [self networkingRed:params];

    [HTTPManager getNetworkRequest:CJStringWithFormat(@"%@%@",[NSString stringWithFormat:@"%@/ntocc-tms-api",[ReAddressDynamic shared].baseUrl],ORDER_DETAIL) withParameter:params
                            withVC:self netName:@"我的运单中心我的运单中心" success:^(id response, NSString *msg) {
        if ([response[@"records"] count] != 0) {
            
            [weakSelf.dataArray addObjectsFromArray:[YunDanCenterModel mj_objectArrayWithKeyValuesArray:response[@"records"]]];
            [weakSelf.tableView.mj_header endRefreshing];
            [weakSelf.tableView.mj_footer endRefreshing];
            
            weakSelf.foundImageView.hidden = YES;
            weakSelf.tableView.hidden = NO;
            
        }else{
            if (_pageNum == 1) {
                weakSelf.foundImageView.hidden = NO;
//                weakSelf.tableView.hidden = YES;
            }
            
            [weakSelf.tableView.mj_header endRefreshing];
            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            
        }
        
//        if (self.delegate && [self.delegate respondsToSelector:@selector(getBadgeNumberInfo:)]) {
//            [self.delegate getBadgeNumberInfo:response[@"sumObject"]];
//        }
        
        [weakSelf.tableView reloadData];
        
    } failure:^(NSInteger code, NSString *msg) {
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
}
- (void)networkingRed:(NSDictionary *)dic{
    [HTTPManager getNetworkRequest:CJStringWithFormat(@"%@%@",[NSString stringWithFormat:@"%@/ntocc-tms-api",[ReAddressDynamic shared].baseUrl],@"/orderDetail/sumData") withParameter:dic
                            withVC:self netName:@"红点" success:^(id response, NSString *msg) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(getBadgeNumberInfo:)]) {
            [self.delegate getBadgeNumberInfo:response];
        }

    } failure:^(NSInteger code, NSString *msg) {
        
    }];
}
- (void)setupBaseView {
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    if ([self.orderStatus isEqualToString: @"7"]) {
            _tableView.frame = CGRectMake(0, 10, ScreenW, ScreenH - TabBarHeight -40 - 44 - 50 - MenuBarHeight);
        [self.view addSubview:self.allButton];
    }else{
        _tableView.frame = CGRectMake(0, 10, ScreenW, ScreenH - TabBarHeight - 10 - 44);
    }
    //弹出菜单
    // Do any additional setup after loading the view.
    //    YCMenuAction *action = [YCMenuAction actionWithTitle:@"暂停" image:image(@"gcy_icon_yundan_zanting") handler:^(YCMenuAction *action) {
    //        NSLog(@"点击了%@",action.title);
    //    }];
    //    YCMenuAction *action1 = [YCMenuAction actionWithTitle:@"删除" image:image(@"gcy_icon_yundan_delete") handler:^(YCMenuAction *action) {
    //        NSLog(@"点击了%@",action.title);
    //    }];
    //    YCMenuAction *action2 = [YCMenuAction actionWithTitle:@"调车" image:image(@"gcy_icon_yundan_tiaoche") handler:^(YCMenuAction *action) {
    //        NSLog(@"点击了%@",action.title);
    //    }];
    //    YCMenuAction *action3 = [YCMenuAction actionWithTitle:@"调价" image:image(@"gcy_icon_yundan_tiaojia") handler:^(YCMenuAction *action) {
    //        NSLog(@"点击了%@",action.title);
    //    }];
    //    YCMenuAction *action4 = [YCMenuAction actionWithTitle:@"派单" image:image(@"gcy_icon_yundan_paidan") handler:^(YCMenuAction *action) {
    //        NSLog(@"点击了%@",action.title);
    //    }];
    //    YCMenuAction *action5 = [YCMenuAction actionWithTitle:@"编辑" image:image(@"gcy_icon_yundan_edit") handler:^(YCMenuAction *action) {
    //        NSLog(@"点击了%@",action.title);
    //    }];
    //
    //    self.menuArray = [[NSMutableArray alloc] initWithObjects:action,action1,action2,action3,action4,action5, nil];
    
}



//上拉刷新数据
- (void)loadNewData{
    _pageNum = 1;
    [self yunDanListRequest:self.notDic];
}
- (void)loadMoreData{
    _pageNum ++;
    [self yunDanListRequest:self.notDic];
}

#pragma mark - YunDanListButtonDelegate
- (void)buttonIndexClick:(NSInteger)index withButton:(UIButton *)button {
    
    NSIndexPath *indexpath = [self.tableView indexPathForRowAtPoint:[self.tableView convertPoint:button.center fromView:button.superview]];
    DebugLog(@"点击了第%ld个item的第%ld个按钮",(long)indexpath.row, (index-99));
    
    if (self.dataArray.count == 0) {
        return;
    }
    
    YunDanCenterModel *ydModel = self.dataArray[indexpath.row];
    _orderCode = ydModel.code;
    
    _statusStr = button.titleLabel.text;
    
    if ([_statusStr isEqual:@"再来一单"]) {
        [HTTPManager getNetworkRequest:CJStringWithFormat(@"%@/ntocc-tms-api/orderDetail/canAgainReceive", [ReAddressDynamic shared].baseUrl)
                         withParameter:@{@"code": ydModel.mainOrderCode}
                                withVC:self
                               netName:@"是否可以再次接单"
                               success:^(id response, NSString *msg) {
            
            HuoYuanDetailViewController *hydvc = [[HuoYuanDetailViewController alloc] init];
            hydvc.orderCode = ydModel.mainOrderCode;
            [self.navigationController pushViewController:hydvc animated:YES];
            
        } failure:^(NSInteger code, NSString *msg) {
            
        }];
    }
    
    if ([_statusStr isEqual:@"接单"]) {
        [CheckInviterView shared].delegate = self;
        [CheckInviterView shared].title = @"接单";
        [CheckInviterView shared].message = @"请确认是否接单";
        [[CheckInviterView shared] show];
    }
    
    if ([_orderStatus isEqual:@"0"]) {
        if ([_statusStr isEqual:@"取消"]) {
            //不带描述的取消弹窗
            [ReasonAndDesAlert shared].delegate = self;
            [ReasonAndDesAlert shared].alertType = 1;
            [[ReasonAndDesAlert shared] show];
            
        } else if ([_statusStr isEqual:@"装货"]) {
            
            ZhuangHuoViewController *zhvc = [[ZhuangHuoViewController alloc] init];
            zhvc.ydModel = ydModel;
            zhvc.orderCode = ydModel.code;
            zhvc.status = 0;
            zhvc.coalUnit = ydModel.coalUnit;
            zhvc.branchCode = ydModel.branchCode;
            zhvc.shippingNoteNumber = ydModel.shippingNoteNumber;
            zhvc.startCountrySubdivisionCode = ydModel.shipperSubdivisionCode;
            zhvc.endCountrySubdivisionCode = ydModel.consigneeSubdivisionCode;
            zhvc.loadingReceiptList = ydModel.loadingReceiptList;
            zhvc.unloadReceiptList = ydModel.unloadReceiptList;
            [self.navigationController pushViewController:zhvc animated:YES];
            
        } else if ([_statusStr isEqual:@"卸货"]) {
            
            ZhuangHuoViewController *zhvc = [[ZhuangHuoViewController alloc] init];
            zhvc.ydModel = ydModel;
            zhvc.orderCode = ydModel.code;
            zhvc.status = 1;
            zhvc.coalUnit = ydModel.coalUnit;
            zhvc.branchCode = ydModel.branchCode;
            zhvc.shippingNoteNumber = ydModel.shippingNoteNumber;
            zhvc.startCountrySubdivisionCode = ydModel.shipperSubdivisionCode;
            zhvc.endCountrySubdivisionCode = ydModel.consigneeSubdivisionCode;
            zhvc.loadingReceiptList = ydModel.loadingReceiptList;
            zhvc.unloadReceiptList = ydModel.unloadReceiptList;
            [self.navigationController pushViewController:zhvc animated:YES];
            
        } else if ([_statusStr isEqual:@"派单"]) {
            //车队可派单给车队司机（只能派给一个司机不能多选）
            PaiDanViewController *pdvc = [[PaiDanViewController alloc] init];
            pdvc.infoFee = ydModel.infoFee;
            pdvc.driverIsShare = ydModel.driverIsShare;
            pdvc.orderCode = ydModel.code;
            pdvc.branchCode = ydModel.branchCode;
            pdvc.status = 0;
            [self.navigationController pushViewController:pdvc animated:YES];
            
        } else if ([_statusStr isEqual:@"取消确认"]) {
            
            //带描述的取消弹窗
            [ReasonAndDesAlert shared].delegate = self;
            [ReasonAndDesAlert shared].alertType = 2;
            [ReasonAndDesAlert shared].cancelReasons = ydModel.driverApplyRemark;
            [[ReasonAndDesAlert shared] show];
            
        } else if ([_statusStr isEqual:@"调单"]) {
            //车队调单
            PaiDanViewController *pdvc = [[PaiDanViewController alloc] init];
            pdvc.infoFee = ydModel.infoFee;
            pdvc.driverIsShare = ydModel.driverIsShare;
            pdvc.orderCode = ydModel.code;
            pdvc.branchCode = ydModel.branchCode;
            pdvc.status = 1;
            [self.navigationController pushViewController:pdvc animated:YES];
            
        } else if ([_statusStr isEqual:@"投诉货主"]) {
            
            TouSuHuoZhuViewController *tshzvc = [[TouSuHuoZhuViewController alloc] init];
            tshzvc.orderCode = ydModel.code;
            tshzvc.pageState = 0;
            [self.navigationController pushViewController:tshzvc animated:YES];
            
        } else if ([_statusStr isEqual:@"投诉记录"]) {
            
            TouSuListViewController *tslvc = [[TouSuListViewController alloc] init];
            tslvc.orderCode = _orderCode;
            tslvc.pagesFlag = 0;
            [self.navigationController pushViewController:tslvc animated:YES];
            
        } else if ([_statusStr isEqual:@"评价"]) {
            
            if (ydModel.isDriverComment == 0) {
                //未评价
                PingJiaViewController *pjvc = [[PingJiaViewController alloc] init];
                pjvc.orderCode = _orderCode;
                [self.navigationController pushViewController:pjvc animated:YES];
            } else {
                [XSInfoView showInfo:@"该运单已评价" onView:self.view];
            }
            
        } else if ([_statusStr isEqual:@"查看评价"]) {
            
            PingJiaDetailViewController *pjdvc = [[PingJiaDetailViewController alloc] init];
            pjdvc.orderCode = _orderCode;
            pjdvc.sourceType = 0;
            pjdvc.commentType = @"1";
            [self.navigationController pushViewController:pjdvc animated:YES];
            
        } else if ([_statusStr isEqual:@"催款"]) {
            
            [CheckInviterView shared].delegate = self;
            [CheckInviterView shared].title = @"催款确认";
            [CheckInviterView shared].message = @"确认要进行催款吗？(每24小时最多催款一次)";
            [[CheckInviterView shared] show];
            
        }
    }
    else if ([_orderStatus isEqual:@"1"]) {
        //待运输
        DebugLog(@"%@",button.titleLabel.text);
        
        if ([_statusStr isEqual:@"取消"]) {
            //不带描述的取消弹窗
            [ReasonAndDesAlert shared].delegate = self;
            [ReasonAndDesAlert shared].alertType = 1;
            [[ReasonAndDesAlert shared] show];
            
        } else if ([_statusStr isEqual:@"装货"]) {
            
            ZhuangHuoViewController *zhvc = [[ZhuangHuoViewController alloc] init];
            zhvc.ydModel = ydModel;
            zhvc.orderCode = ydModel.code;
            zhvc.status = 0;
            zhvc.coalUnit = ydModel.coalUnit;
            zhvc.branchCode = ydModel.branchCode;
            zhvc.shippingNoteNumber = ydModel.shippingNoteNumber;
            zhvc.startCountrySubdivisionCode = ydModel.shipperSubdivisionCode;
            zhvc.endCountrySubdivisionCode = ydModel.consigneeSubdivisionCode;
            zhvc.loadingReceiptList = ydModel.loadingReceiptList;
            zhvc.unloadReceiptList = ydModel.unloadReceiptList;
            [self.navigationController pushViewController:zhvc animated:YES];
            
        } else if ([_statusStr isEqual:@"派单"]) {
            //车队可派单给车队司机（只能派给一个司机不能多选）
            PaiDanViewController *pdvc = [[PaiDanViewController alloc] init];
            pdvc.infoFee = ydModel.infoFee;
            pdvc.driverIsShare = ydModel.driverIsShare;
            pdvc.orderCode = ydModel.code;
            pdvc.branchCode = ydModel.branchCode;
            pdvc.status = 0;
            [self.navigationController pushViewController:pdvc animated:YES];
            
        } else if ([_statusStr isEqual:@"取消确认"]) {
            //带描述的取消弹窗
            [ReasonAndDesAlert shared].delegate = self;
            [ReasonAndDesAlert shared].alertType = 2;
            [ReasonAndDesAlert shared].cancelReasons = ydModel.driverApplyRemark;
            [[ReasonAndDesAlert shared] show];
            
        } else if ([_statusStr isEqual:@"调单"]) {
            //车队调单
            PaiDanViewController *pdvc = [[PaiDanViewController alloc] init];
            pdvc.infoFee = ydModel.infoFee;
            pdvc.driverIsShare = ydModel.driverIsShare;
            pdvc.orderCode = ydModel.code;
            pdvc.branchCode = ydModel.branchCode;
            pdvc.status = 1;
            [self.navigationController pushViewController:pdvc animated:YES];
            
        } else if ([_statusStr isEqual:@"投诉货主"]) {
            
            TouSuHuoZhuViewController *tshzvc = [[TouSuHuoZhuViewController alloc] init];
            tshzvc.orderCode = ydModel.code;
            tshzvc.pageState = 0;
            [self.navigationController pushViewController:tshzvc animated:YES];
            
        } else if ([_statusStr isEqual:@"投诉记录"]) {
            
            TouSuListViewController *tslvc = [[TouSuListViewController alloc] init];
            tslvc.orderCode = _orderCode;
            tslvc.pagesFlag = 0;
            [self.navigationController pushViewController:tslvc animated:YES];
        }
        
    } else if ([_orderStatus isEqual:@"2"]) {
        //运输中
        if ([_statusStr isEqual:@"卸货"]) {
            
            ZhuangHuoViewController *zhvc = [[ZhuangHuoViewController alloc] init];
            zhvc.ydModel = ydModel;
            zhvc.orderCode = ydModel.code;
            zhvc.status = 1;
            zhvc.coalUnit = ydModel.coalUnit;
            zhvc.branchCode = ydModel.branchCode;
            zhvc.shippingNoteNumber = ydModel.shippingNoteNumber;
            zhvc.startCountrySubdivisionCode = ydModel.shipperSubdivisionCode;
            zhvc.endCountrySubdivisionCode = ydModel.consigneeSubdivisionCode;
            zhvc.loadingReceiptList = ydModel.loadingReceiptList;
            zhvc.unloadReceiptList = ydModel.unloadReceiptList;
            [self.navigationController pushViewController:zhvc animated:YES];
            
        } else if ([_statusStr isEqual:@"派单"]) {
            
            //车队可派单给车队司机（只能派给一个司机不能多选）
            PaiDanViewController *pdvc = [[PaiDanViewController alloc] init];
            pdvc.infoFee = ydModel.infoFee;
            pdvc.driverIsShare = ydModel.driverIsShare;
            pdvc.orderCode = ydModel.code;
            pdvc.branchCode = ydModel.branchCode;
            pdvc.status = 0;
            [self.navigationController pushViewController:pdvc animated:YES];
            
        } else if ([_statusStr isEqual:@"调单"]) {
            
            //车队调单
            PaiDanViewController *pdvc = [[PaiDanViewController alloc] init];
            pdvc.infoFee = ydModel.infoFee;
            pdvc.driverIsShare = ydModel.driverIsShare;
            pdvc.orderCode = ydModel.code;
            pdvc.branchCode = ydModel.branchCode;
            pdvc.status = 1;
            [self.navigationController pushViewController:pdvc animated:YES];
            
        } else if ([_statusStr isEqual:@"投诉货主"]) {
            
            TouSuHuoZhuViewController *tshzvc = [[TouSuHuoZhuViewController alloc] init];
            tshzvc.orderCode = ydModel.code;
            tshzvc.pageState = 0;
            [self.navigationController pushViewController:tshzvc animated:YES];
            
        } else if ([_statusStr isEqual:@"投诉记录"]) {
            
            TouSuListViewController *tslvc = [[TouSuListViewController alloc] init];
            tslvc.orderCode = _orderCode;
            tslvc.pagesFlag = 0;
            [self.navigationController pushViewController:tslvc animated:YES];
        }
        
    } else if ([_orderStatus isEqual:@"3"] || [_orderStatus isEqual:@"4"]) {
        //待结算或待打款
        if ([_statusStr isEqual:@"评价"]) {
            
            if (ydModel.isDriverComment == 0) {
                //未评价
                PingJiaViewController *pjvc = [[PingJiaViewController alloc] init];
                pjvc.orderCode = _orderCode;
                [self.navigationController pushViewController:pjvc animated:YES];
            } else {
                [XSInfoView showInfo:@"该运单已评价" onView:self.view];
            }
            
        } else if ([_statusStr isEqual:@"查看评价"]) {
            
            PingJiaDetailViewController *pjdvc = [[PingJiaDetailViewController alloc] init];
            pjdvc.orderCode = _orderCode;
            pjdvc.sourceType = 0;
            pjdvc.commentType = @"1";
            [self.navigationController pushViewController:pjdvc animated:YES];
            
        } else if ([_statusStr isEqual:@"催款"]) {
            
            [CheckInviterView shared].delegate = self;
            [CheckInviterView shared].title = @"催款确认";
            [CheckInviterView shared].message = @"确认要进行催款吗？(每24小时最多催款一次)";
            [[CheckInviterView shared] show];
            
        } else if ([_statusStr isEqual:@"投诉货主"]) {
            
            TouSuHuoZhuViewController *tshzvc = [[TouSuHuoZhuViewController alloc] init];
            tshzvc.orderCode = ydModel.code;
            tshzvc.pageState = 0;
            [self.navigationController pushViewController:tshzvc animated:YES];
            
        } else if ([_statusStr isEqual:@"投诉记录"]) {
            
            TouSuListViewController *tslvc = [[TouSuListViewController alloc] init];
            tslvc.orderCode = _orderCode;
            tslvc.pagesFlag = 0;
            [self.navigationController pushViewController:tslvc animated:YES];
            
        }
    } else if ([_orderStatus isEqual:@"5"]) {
        //已完成
        if ([_statusStr isEqual:@"评价"]) {
            
            if (ydModel.isDriverComment == 0) {
                //未评价
                PingJiaViewController *pjvc = [[PingJiaViewController alloc] init];
                pjvc.orderCode = _orderCode;
                [self.navigationController pushViewController:pjvc animated:YES];
            } else {
                [XSInfoView showInfo:@"该运单已评价" onView:self.view];
            }
            
        } else if ([_statusStr isEqual:@"查看评价"]) {
            
            PingJiaDetailViewController *pjdvc = [[PingJiaDetailViewController alloc] init];
            pjdvc.orderCode = _orderCode;
            pjdvc.sourceType = 0;
            pjdvc.commentType = @"1";
            [self.navigationController pushViewController:pjdvc animated:YES];
            
        } else if ([_statusStr isEqual:@"投诉记录"]) {
            
            TouSuListViewController *tslvc = [[TouSuListViewController alloc] init];
            tslvc.orderCode = _orderCode;
            tslvc.pagesFlag = 0;
            [self.navigationController pushViewController:tslvc animated:YES];
            
        }
        
    }
    
    if ([_statusStr isEqual:@"异常上报"]) {
        TouSuHuoZhuViewController *tshzvc = [[TouSuHuoZhuViewController alloc] init];
        tshzvc.orderCode = ydModel.code;
        tshzvc.pageState = 1;
        [self.navigationController pushViewController:tshzvc animated:YES];
    }
    
    if([_statusStr isEqual:@"异常处理"]){
        ZTXYiChangChuLiListController *tshzvc = [[ZTXYiChangChuLiListController alloc] init];
        tshzvc.orderCode = ydModel.code;
        [self.navigationController pushViewController:tshzvc animated:YES];
    }
    if ([_statusStr isEqual:@"超10单处理"]) {
        ZhuangHuoViewController *zhvc = [[ZhuangHuoViewController alloc] init];
        zhvc.ydModel = ydModel;
        zhvc.orderCode = ydModel.code;
        zhvc.status = 1;
        zhvc.coalUnit = ydModel.coalUnit;
        zhvc.branchCode = ydModel.branchCode;
        zhvc.shippingNoteNumber = ydModel.shippingNoteNumber;
        zhvc.startCountrySubdivisionCode = ydModel.shipperSubdivisionCode;
        zhvc.endCountrySubdivisionCode = ydModel.consigneeSubdivisionCode;
        zhvc.loadingReceiptList = ydModel.loadingReceiptList;
        zhvc.unloadReceiptList = ydModel.unloadReceiptList;
        zhvc.isSumTen = @"1";//超10单处理方法
        [self.navigationController pushViewController:zhvc animated:YES];
    }
    
}

- (void)CYNormalAlertViewBtnClick:(NSInteger)btnIndex {
    WEAKSELF
    if (btnIndex == 301) {
        
        if ([_statusStr isEqual:@"接单"]) {
            
            [HTTPManager putNetworkRequest:CJStringWithFormat(@"%@/ntocc-tms-api/orderDetail/teamDriverReceiveOrderDetail", [ReAddressDynamic shared].baseUrl) withParameter:@{@"code": _orderCode} withVC:self netName:@"车队司机、加入车队独立司机接运输单" success:^(id response, NSString *msg) {
                
                [XSInfoView showInfo:msg onView:weakSelf.view];
                
                [[CheckInviterView shared] hidden];
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.7 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
                    [weakSelf yunDanListRequest:self.notDic];
                });
                
            } failure:^(NSInteger code, NSString *msg) {
                
                [[CheckInviterView shared] hidden];
                
            }];
            [@{} stringValueForKey:@"" default:@""];
        }
        
        if ([_orderStatus isEqual:@"0"] || [_orderStatus isEqual:@"3"] || [_orderStatus isEqual:@"4"]) {
            //待结算 待打款
            if ([_statusStr isEqual:@"催款"]) {
                
                NSDictionary *params = @{@"orderDetailCode": _orderCode,
                                         @"token":[UserDefaultUtil valueForKey:USER_TOKEN],
                };
                
                [HTTPManager postNetworkRequest:CJStringWithFormat(@"%@%@", [NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl], URGEREMIT_PROMPT) withParameter:params withVC:self netName:@"司机催款" success:^(id response, NSString *msg) {
                    
                    [[CheckInviterView shared] hidden];
                    
                    [XSInfoView showInfo:@"催款成功" onView:weakSelf.view];
                    
                    //                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
                    //
                    //                    });
                    
                } failure:^(NSInteger code, NSString *msg) {
                    
                    [[CheckInviterView shared] hidden];
                }];
            }
        }
    }
    
}

/// 选择承运车辆
/// @param block 回调
- (void)getVehicleCode:(VehileCodeBlock)block {
    NSDictionary *pamars = @{
        @"authStatusExt": @3,
        @"pageSize": @"9999",
        @"pageNum": @1,
    };
    
    NSString *listUrl = CJStringWithFormat(@"%@%@", [NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl], @"/vehicle/bindList");
    
    @weakify(self);
    [HTTPManager jsonNetworkRequestWithMethod:@"GET"
                                    urlString:listUrl
                                withParameter:pamars
                                       withVC:self
                                      netName:@"车辆列表"
                                      success:^(id response, NSString *msg) {
        @strongify(self);
        NSArray *vehicleArr = [DriverVehicleModel mj_objectArrayWithKeyValuesArray:response[@"records"]];
        if (vehicleArr.count == 0) {
            [XSInfoView showInfo:@"没有查询到审核通过的车辆信息" onView:self.view];
            return;
        }
        NSMutableArray *arr = [NSMutableArray array];
        for (DriverVehicleModel *model in vehicleArr) {
            [arr addObject:model.licenseNumber];
        }
        
        BRStringPickerView *stringPickerView = [[BRStringPickerView alloc] initWithPickerMode:BRStringPickerComponentSingle];
        stringPickerView.title = @"选择承运车辆";
        stringPickerView.dataSourceArr = arr;
        stringPickerView.resultModelBlock = ^(BRResultModel *resultModel) {
            DriverVehicleModel *vehicle = vehicleArr[resultModel.index];
            NSString *vehicleCode = vehicle.code;
            if (block) {
                block(vehicleCode);
            }
        };
        
        BRPickerStyle *customStyle = [BRPickerStyle pickerStyleWithThemeColor:Theme_ColorValue];
        customStyle.topCornerRadius = 7;
        stringPickerView.pickerStyle = customStyle;
        [stringPickerView show];
        
    } failure:^(NSInteger code, NSString *msg) {
        
    }];
}

#pragma mark - CancelAlertViewDeletage
- (void)ReturnCancelReasonDes:(NSString *)reason andDes:(NSString *)des andBtnIndex:(NSInteger)index {
    WEAKSELF
    
    NSMutableDictionary *parmas = [[NSMutableDictionary alloc] init];
    
    if (index == 300) {
        //取消或拒绝
        if ([ReasonAndDesAlert shared].alertType == 1) {
            
            [[ReasonAndDesAlert shared] hidden];
            return;
            
        } else if ([ReasonAndDesAlert shared].alertType == 2) {
            
            if ([DCSpeedy isBlankString:des]) {
                [XSInfoView showInfo:@"请填写处理描述" onView:self.view.window];
                return;
            }
            
            [parmas setValue:_orderCode forKey:@"code"];
            [parmas setValue:@"0" forKey:@"cancelType"];
            [parmas setValue:@"3" forKey:@"isCancel"];
            [parmas setValue:reason forKey:@"driverApplyRemark"];
            [parmas setValue:des forKey:@"shipperDealRemark"];
        }
        
    } else {
        
        [parmas setValue:_orderCode forKey:@"code"];
        [parmas setValue:@"0" forKey:@"cancelType"];
        [parmas setValue:reason forKey:@"driverApplyRemark"];
        [parmas setValue:des forKey:@"shipperDealRemark"];
        
        if ([ReasonAndDesAlert shared].alertType == 1) {
            
            if ([DCSpeedy isBlankString:reason] == YES) {
                [XSInfoView showInfo:@"请填写取消原因" onView:self.view.window];
                return;
            }
            
            [parmas setValue:@"1" forKey:@"isCancel"];
            
        } else {
            
            if ([DCSpeedy isBlankString:des]) {
                [XSInfoView showInfo:@"请填写处理描述" onView:self.view.window];
                return;
            }
            [parmas setValue:@"2" forKey:@"isCancel"];
        }
        
    }
    
    //确定
    if ([_orderStatus isEqual:@"0"] || [_orderStatus isEqual:@"1"]) {
        if ([_statusStr isEqual:@"取消确认"] || [_statusStr isEqual:@"取消"]) {
            
            [parmas setValue:[UserDefaultUtil valueForKey:USER_TOKEN] forKey:@"token"];
            
            [HTTPManager putNetworkRequest:CJStringWithFormat(@"%@%@",[NSString stringWithFormat:@"%@/ntocc-tms-api",[ReAddressDynamic shared].baseUrl],ORDER_DETAIL_DRIVERCANCELORDER) withParameter:parmas withVC:self netName:@"取消/取消确认/取消拒绝" success:^(id response, NSString *msg) {
                
                [XSInfoView showInfo:msg onView:self.view.window];
                
                [[ReasonAndDesAlert shared] hidden];
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
                    [weakSelf yunDanListRequest:self.notDic];
                });
                
            } failure:^(NSInteger code, NSString *msg) {
                
            }];
            
        }
    }
    
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YunDanCenterCell *cell = [tableView dequeueReusableCellWithIdentifier:YunDanCenterCellID];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.delegate = self;
    
    cell.orderStatus = self.orderStatus;
    
    cell.assignmentStatus = self.assignmentStatus;
    
    if (self.dataArray.count != 0) {
        
        cell.model = self.dataArray[indexPath.row];
        
    }
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *shipmentName = @"";
    NSString *vehicleLicense = @"";
    if (self.dataArray.count != 0) {
        shipmentName = [self.dataArray[indexPath.row] shipmentName];
        vehicleLicense = [self.dataArray[indexPath.row] vehicleLicense];
    }
    if ([_orderStatus isEqual:@"6"]) {
        if ([shipmentName isNotBlank]) {
            return 125 + 50;
        } else {
            return 125;
        }
    } else {
        if ([shipmentName isNotBlank]) {
            if (![vehicleLicense isNotBlank]) {
                return cellHeight - 30;
            }
            return cellHeight;
        } else {
            return cellHeight - 30;
        }
    }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (![_orderStatus isEqual:@"6"]) {
        YunDanCenterDetailViewController *ydcdvc = [[YunDanCenterDetailViewController alloc] init];
        ydcdvc.assignmentStatus = self.assignmentStatus;
        YunDanCenterModel *model = self.dataArray[indexPath.row];
        ydcdvc.orderCode = model.code;
        ydcdvc.ydModel = model;
        [self.navigationController pushViewController:ydcdvc animated:YES];
    }
    
}

#pragma mark - LazyLoad
-(UIButton *)allButton{
    if (!_allButton) {
        self.allButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
        self.allButton.frame =CGRectMake(30, ScreenH - TabBarHeight - MenuBarHeight - 10 - 44 - 50, ScreenW - 60, 50);
        self.allButton.layer.masksToBounds = YES;
        self.allButton.layer.cornerRadius = 10;
        self.allButton.layer.cornerRadius = 25;
        self.allButton.backgroundColor = Green_Color;
        [self.allButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        [self.allButton setTitle:@"批量派单" forState:UIControlStateNormal];
        [self.allButton addTarget:self action:@selector(allButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _allButton;
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.frame = CGRectMake(0, 10, ScreenW, ScreenH - TabBarHeight - 10 - 44);
        [self.view addSubview:_tableView];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        
        //注册单元格
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YunDanCenterCell class]) bundle:nil] forCellReuseIdentifier:YunDanCenterCellID];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    }
    
    return _tableView;
}
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

- (NSMutableArray *)menuArray {
    if (!_menuArray) {
        _menuArray = [[NSMutableArray alloc] init];
    }
    return _menuArray;
}

- (UIImageView *)foundImageView {
    if (!_foundImageView) {
        _foundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 190, 190)];
        _foundImageView.image = image(@"gcy_img_not_found");
        _foundImageView.contentMode = UIViewContentModeCenter;
        _foundImageView.center = self.view.center;
        _foundImageView.cy_y -=80;
        UILabel *textL = [[UILabel alloc] initWithFrame:CGRectMake(0, 170, 190, 20)];
        textL.text = @"暂无数据";
        textL.textAlignment = NSTextAlignmentCenter;
        textL.textColor = [UIColor colorWithHexString:@"#ADB6CC"];
        textL.font = [UIFont systemFontOfSize:12.0f];
        [_foundImageView addSubview:textL];
        [self.view addSubview:_foundImageView];
    }
    return _foundImageView;
}
- (void)allButtonAction{
    if (self.dataArray.count > 0) {
        YnuDanBatchPaiDViewController *batchVC = [[YnuDanBatchPaiDViewController alloc] init];
        YunDanCenterModel *model = self.dataArray.firstObject;
        batchVC.createdBy = model.createdBy;
        [self.navigationController pushViewController:batchVC animated:YES];
    }else{
        [XSInfoView showInfo:@"没有待派单" onView:self.view.window];
    }
}

@end
